/*******************************************************************************
 * Copyright (c) 2012, 2013 XML-Print Project.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     XML-Print Project - initial API and implementation
 ******************************************************************************/
package info.xmlprint.ui.widgets;

import org.apache.log4j.Logger;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;

public class ProgressDialog {

	private Logger logger = Logger.getLogger(this.getClass().getName());

	private boolean stopFlag;//

	private IRunnableWithProgress runnable;

	public ProgressDialog() {
		this.stopFlag = false;

		runnable = new IRunnableWithProgress() {
			public void run(IProgressMonitor monitor) {

				monitor.beginTask("Transformierung......",
						IProgressMonitor.UNKNOWN);

				while (true) {
					//
					if (monitor.isCanceled() || stopFlag) {
						stopFlag = true; //
						break;//
					}

					try {
						Thread.sleep(99);
					} catch (Throwable t) {
						logger.error("", t);

					}
					monitor.worked(1);//
				}
				monitor.done();//
			}
		};

	}

	public IRunnableWithProgress getRunnable() {
		return runnable;
	}

	public void startRunnable() {
		try {
			new ProgressMonitorDialog(null).run(true, true, runnable);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void stopRunnable() {
		stopFlag = true;
	}

	public static void main(String args[]) {
		final ProgressDialog pd = new ProgressDialog();
		new Thread() {
			public void run() {
				pd.startRunnable();
			}
		}.start();

		pd.stopRunnable();

	}
}
